<template>
	<view class="conatiner">
		<view class="navbar-bg"></view>
		<u-sticky>
			<view class="x-p-l-32 x-p-r-32 x-p-t-32 u-p-b-20" :class="isNavbar?'navbar':''">
				<u-status-bar></u-status-bar>
				<view class="flex align-center">
					<view>
						<u-image :showLoading="true" mode="aspectFill" src="@/static/index/logo.png" width="58rpx"
							height="58rpx">
							<view slot="loading">
								<u-loading-icon></u-loading-icon>
							</view>
						</u-image>
					</view>
					<text class="u-m-l-20 u-m-r-20"></text>
					<text @click="getlocation" class="u-m-r-10"
						style="color: #181919;">{{address?address:'点击定位'}}</text>
					<u-icon name="play-right-fill" size="10" color="#181919"></u-icon>
				</view>
			</view>
		</u-sticky>

		<view class="x-p-l-32 x-p-r-32">
			<u-swiper :list="swiper_model" height="360rpx" keyName="imageUrl" @click="handleSwiperClick"
				indicator></u-swiper>
		</view>

		<view class="u-m-t-20 u-p-l-10 u-p-r-10">
			<u-grid :border="false" col="5">
				<u-grid-item v-for="(model,k) in type_model" :key="k">
					<view @click="$u.route(model.path)"
						class="flex align-center justify-center flex-direction u-m-b-20">
						<u-icon :name="model.imageUrl" size="92rpx"></u-icon>
						<view class="u-font-xs text-black u-m-t-10 ">{{model.title}}</view>
					</view>
				</u-grid-item>

			</u-grid>
		</view>



		<!-- <view class="x-m-l-32 x-m-r-32 u-p-l-20 flex u-m-t-10 cell2">
			<view @click="handleHeXiao()" class="flex align-center" style="width: 50%;">
				<view class="u-m-r-40">
					<view class="name">团券核销</view>
					<view class="tips">平台预约核销</view>
				</view>
				<u-image :showLoading="true" mode="aspectFill" src="@/static/personal/p-yhj.png" width="92rpx"
					height="84rpx">
					<view slot="loading">
						<u-loading-icon></u-loading-icon>
					</view>
				</u-image>
			</view>
			<view class="line"></view>
			<view @click="$u.route('/pages/index/partnership/partnership')" class="flex align-center u-m-l-20"
				style="width: 50%;">
				<view class="u-m-r-40">
					<view class="name">合伙人招募</view>
					<view class="tips">点击入驻赚现金</view>
				</view>
				<u-image :showLoading="true" mode="aspectFill" src="@/static/index/cell-hhzm.png" width="76rpx"
					height="78rpx">
					<view slot="loading">
						<u-loading-icon></u-loading-icon>
					</view>
				</u-image>
			</view>
		</view> -->

		<block v-for="(type_obj,j) in show_type_model" :key="j">
			<view class="card g-red">
				<view class="flex align-center u-row-between">
					<view class="flex align-center">
						<view class="title">{{type_obj.title}}</view>
						<view class="note">{{type_obj.remark||''}}</view>
					</view>
					<view @click="$u.route('/pages/index/category/category?busType=0')" style="color: #007aff;">更多
					</view>
				</view>

				<view class="u-m-t-20 product">
					<block v-for="(item,k) in hotShowType" :key="k">
						<view v-if="k<6" style="margin-right: 12rpx;">
							<ProCol :tag="true" :productInfo="item"></ProCol>
						</view>
					</block>
				</view>
			</view>
		</block>


		<!-- <view class="card bg-white" v-if="firstProduct.length>0">
			<view class="flex align-center">
				<view class="green-title">首单优惠</view>
				<view class="note">新用户立减</view>
			</view>
			<view class="u-m-t-20 product">
				<view v-for="(item,k) in firstProduct" :key="k" style="margin-right: 12rpx;">
					<ProColFirst :tag="true" :productInfo="item"></ProColFirst>
				</view>
			</view>
		</view>

		<view class="card bg-white" v-if="miaoshaProduct.length>0">
			<view class="flex align-center">
				<view class="green-title">限时秒杀</view>
				<view class="note">每天限时秒杀</view>
			</view>
			<view class="u-m-t-20 product">
				<view v-for="(item,k) in miaoshaProduct" :key="k" style="margin-right: 12rpx;">
					<ProColMiaosha :tag="true" :productInfo="item"></ProColMiaosha>
				</view>
			</view>
		</view> -->
		<login ref="login" @refresh="refresh"></login>
	</view>
</template>

<script>
	import ProCol from '@/components/x-unit/product/pro-col.vue'
	import login from '@/components/x-unit/login/login.vue'
	import ProColMiaosha from '@/components/x-unit/product/pro-col-miaosha.vue'
	import ProColFirst from '@/components/x-unit/product/pro-col-first.vue'

	import {
		getIndexList,
		getCityInfoByLatLng
	} from '@/common/api/api.js'

	export default {
		components: {
			ProCol,
			login,
			ProColMiaosha,
			ProColFirst

		},
		data() {
			return {
				address: '',
				isNavbar: false,
				swiper_model: [], //轮播图片
				type_model: [], //分类
				show_type_model: [], //热门推荐模块
				index_bottom: [], //底部对比
				hotShowType: [],
				miaoshaProduct: [],
				firstProduct: [],
				list1: [
					'/static/index/banner1.png',
					'/static/index/banner1.png',
					'/static/index/banner1.png',
				]
			}
		},
		onLoad(options) {
			let tuijianUser = options.tuijianUser;
			if (tuijianUser) {
				this.$store.commit('$uStore', {
					name: '$tuijianUser',
					value: tuijianUser
				});
			}
			this.getIndexList();
		},
		onShow() {
			this.getlocationIndex();
			if (!this.$store.state.$token) {
				setTimeout(() => {
					this.$refs.login.open();
				}, 100);
			} else {
				// if (this.$store.state.$userInfo.isShiming == 0) {
				// 	setTimeout(() => {
				// 		uni.$u.route('/pages/personal/userInfo/userInfo');
				// 	}, 100);
				// }
			}

		},
		// 1.发送给朋友
		onShareAppMessage(res) {
			let lifeData = uni.getStorageSync('lifeData');
			return {
				title: "首页",
				path: '/pages/index/index?tuijianUser=' + lifeData.$token,
			}
		},
		//2.分享到朋友圈
		onShareTimeline(res) {
			let lifeData = uni.getStorageSync('lifeData');
			return {
				title: "首页",
				path: '/pages/index/index?tuijianUser=' + lifeData.$token,
			}
		},
		methods: {
			// 核销
			handleHeXiao() {
				//$u.route('/pages/personal/reservation/reservation')
				uni.$u.toast('功能开发中。。');
			},
			refresh() {
				this.getIndexList();
			},
			videoPlay(index) {
				if (this.playIdx !== '') {
					console.log(`暂停上⼀个下标为${this.playIdx}的视频`);
					let videoContext = uni.createVideoContext(`video${this.playIdx}`)
					videoContext.pause()
				}
				this.playIdx = index
			},
			// 轮播点击
			handleSwiperClick(index) {

				this.$u.route(this.swiper_model[index].path);
			},
			//首页信息
			getIndexList() {
				getIndexList().then(res => {
					let data = res.data;
					this.type_model = data.type_model;
					this.swiper_model = data.swiper_model;
					this.show_type_model = data.show_type_model;
					this.hotShowType = data.hotShowType.map(item => {
						item.showImg = JSON.parse(item.indexImg)[0];
						return item;
					});
					this.firstProduct = data.firstProduct.map(item => {
						item.showImg = JSON.parse(item.indexImg)[0];
						return item;
					});
					this.miaoshaProduct = data.miaoshaProduct.map(item => {
						item.showImg = JSON.parse(item.indexImg)[0];
						return item;
					});

					this.index_bottom = data.index_bottom;
				})
			},
			change(e) {
				this.keyName = e.id;
			},
			//获取坐标
			getlocation() {
				let that = this;
				uni.getLocation({
					type: 'wgs84', //gcj02
					// altitude: true,
					// geocode:true,
					success: function(res) {
						let lat = res.latitude;
						let lng = res.longitude;
						that.getCityInfoByLatLng(lat, lng);
						uni.$u.toast('定位更新成功');
						console.log("lat,lan", lat, lng, res);
					},
					fail: function(e) {
						console.log('定位失败')
						console.log(e)
					}
				})
			},
			getlocationIndex() {
				let that = this;
				uni.getLocation({
					type: 'wgs84', //gcj02
					// altitude: true,
					// geocode:true,
					success: function(res) {
						let lat = res.latitude;
						let lng = res.longitude;
						that.getCityInfoByLatLng(lat, lng);
						console.log("lat,lan", lat, lng, res);
					},
					fail: function(e) {
						console.log('定位失败')
						console.log(e)
					}
				})
			},
			//获取具体地址
			getCityInfoByLatLng(lat, lng) {
				getCityInfoByLatLng({
					lat,
					lng
				}).then(res => {
					this.address = res.data.streetNumber;
				})
			}
		},
		onPageScroll(e) {
			if (e.scrollTop > 20) {
				this.isNavbar = true;
			} else {
				this.isNavbar = false;
			}
		},
		onPullDownRefresh() {
			this.getIndexList();
			this.getlocation();
			uni.hideLoading();
		}
	}
</script>

<style lang="scss">
	.navbar-bg {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		z-index: -1;
		height: 176rpx;
		// background: linear-gradient(90deg, #C8CDFF 0%, #CBFFFA 54%, #FF5B00 100%);
		background: linear-gradient(90deg, #FFE8DB 0%, #FFE8DB 54%, #FF8B4A 100%);
		border-radius: 0px 0px 0px 0px;
		filter: blur(50rpx);
	}

	.navbar {
		// background: linear-gradient(90deg, #C8CDFF 0%, #CBFFFA 54%, #FF5B00 100%);
		background: linear-gradient(90deg, #FFE8DB 0%, #FFE8DB 54%, #FF8B4A 100%);
	}

	.cell2 {
		width: 686rpx;
		height: 140rpx;
		background: linear-gradient(90deg, #F6FFFB 0%, #FFFFFF 50%, #FFFFFF 100%);
		border-radius: 16rpx 16rpx 16rpx 16rpx;
		border: 2rpx solid #FFFFFF;

		.name {
			font-size: 28rpx;
			font-weight: 800;
			color: #333333;
		}

		.tips {
			font-size: 24rpx;
			color: #F97D3F;
		}

		.line {
			width: 0px;
			height: 80rpx;
			opacity: 1;
			border: 2rpx solid #EEEEEE;
			margin-top: 30rpx;
		}
	}

	.card {
		padding-top: 24rpx;
		padding-bottom: 28rpx;
		padding-left: 20rpx;
		padding-right: 20rpx;
		margin-left: 32rpx;
		margin-right: 32rpx;
		margin-top: 20rpx;
		width: 686rpx;
		min-height: 446rpx;
		border-radius: 16rpx;
		border: 2rpx solid #FFFFFF;

		.title {
			position: relative;
			z-index: 1;
			font-size: 36rpx;
			font-weight: normal;
			color: #282828;
			font-weight: 800;
		}

		.title::before {
			position: absolute;
			bottom: 4rpx;
			z-index: -1;
			content: '';
			width: 90rpx;
			height: 8rpx;
			background: linear-gradient(90deg, #FD6140 0%, rgba(253, 96, 61, 0) 100%);
			border-radius: 0px 0px 0px 0px;
		}

		.green-title {
			position: relative;
			z-index: 1;
			font-size: 36rpx;
			font-weight: normal;
			color: #282828;
			font-weight: 800;
		}

		.green-title::before {
			position: absolute;
			bottom: 4rpx;
			z-index: -1;
			content: '';
			width: 90rpx;
			height: 8rpx;
			background: linear-gradient(90deg, #FF5B00 0%, rgba(129, 216, 207, 0) 100%);
			border-radius: 0px 0px 0px 0px;
		}


		.note {
			font-size: 24rpx;
			color: #999999;
			margin-left: 12rpx;
		}

		.label {
			font-size: 32rpx;
			font-weight: 800;
			color: #333333;
		}
	}

	.g-red {
		background: linear-gradient(180deg, #FFF4F2 0%, #FFFFFF 50%, #FFFFFF 100%);
		opacity: 1;
	}

	.product {
		display: flex;
		align-items: center;
		flex-wrap: wrap;
		// overflow-y: scroll;
	}
</style>